#include "scan.h"

Scan::Scan() {
    ip=NetAction::getIPAddress();
    hostName = NetAction::getTempName();
    terminalType = QString::fromStdString( getTerminalType());
    protocolType = SCAN_MESSAGE_REQ;//默认使用请求
}

string Scan::getTerminalType(){
#ifdef _WIN32
    return "WIN";
#elif __linux__
    return "LINUX";
#endif
}


QByteArray Scan::enCodeToJson(Scan *scan){
    QJsonObject json;
    json["ip"] =  QJsonValue(scan->ip);
    json["hostName"] = QJsonValue(scan->hostName);
    json["terminalType"] = QJsonValue(scan->terminalType);
    json["protocolType"] = QJsonValue(scan->protocolType);

    QJsonDocument jsonDoc(json);
    QString jsonStr = jsonDoc.toJson(QJsonDocument::Compact);
    QDEBUG_FILE_FUNC<<jsonStr;
    return jsonStr.toUtf8();

}

Scan  Scan::deCodeFromJson(QByteArray jsonData){
    QJsonParseError err;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData,&err);
    QJsonObject obj = jsonDoc.object();
    QVariantMap jsonMap = obj.toVariantMap();
    return Scan::fromVariantMap(jsonMap);

}

 Scan Scan::fromVariantMap(const QVariantMap & map){
    Scan scan;
    scan.ip = map.value("ip").toString();
    scan.hostName = map.value("hostName").toString();
    scan.terminalType = map.value("terminalType").toString();
    scan.protocolType = map.value("protocolType").toInt();
    return scan;
 }


